package com.sp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sp.model.*;  

public class LoginClServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginClServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

        /*String u=request.getParameter("name");  
        String p=request.getParameter("password");  
        UserBeanCl ubc=new UserBeanCl();  
        //System.out.println("servlet");  
        if(ubc.checkUser(u, p))  
        {  
            ArrayList<UserBean> al=ubc.getUsersByPage(1);  
            int pageCount=ubc.getPageCount();  
            request.setAttribute("result", al);  
            request.setAttribute("pageCount", pageCount);  
            request.setAttribute("pageNow", 1);  
            request.getSession().setAttribute("myName", u);  
              
            request.getRequestDispatcher("index.html").forward(request, response);  
  
        }  
        else  
        {  
        	
            request.getRequestDispatcher("login.jsp").forward(request, response);  
        }  */
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String uname = request.getParameter("name")!=null?request.getParameter("name"):"";
		String password = request.getParameter("password")!=null?request.getParameter("password"):"";
		boolean checked =  new UserBeanCl().checkUser(uname, password);
		PrintWriter pw = response.getWriter();
		if(checked==true){
			HttpSession session = request.getSession();
			session.setAttribute("loginFlag", 1);
		}else{
			pw.write("<script>alert('Your username or password is wrong!');window.location.href='login.jsp';</script>");
			return;
		}
		
		pw.write("<script>window.location.href='index.html';</script>");
		return;
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
